#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import json
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
import requests

def get_proxy_ip():
    try:
        res=requests.get("http://127.0.0.1:5000/get")
        if res.status_code==200:

            proxy = "http://"+res.text
            return proxy
        else:
            get_proxy_ip()
    except Exception as e:
        print(e)
        get_proxy_ip()

chromeOptions = webdriver.ChromeOptions()

# 设置代理
chromeOptions.add_argument("--proxy-server="+get_proxy_ip())
# 一定要注意，=两边不能有空格，不能是这样--proxy-server = http://202.20.16.82:10152
browser = webdriver.Chrome(chrome_options = chromeOptions)
from xueqiu.spider_zuhe import main
# browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10);
import os

class updateCookies(object):

    def update(self):
        browser.get("https://xueqiu.com/P/ZH1253585")
        # cube - content
        # user_name = wait.until(
        #     EC.presence_of_element_located((By.CSS_SELECTOR, "cube-content"))
        # )
        results = browser.get_cookies();
        cookie = {}
        for res in results:
            cookie[res.get("name")] = res.get("value")
        print(cookie)
        path=os.getcwd()+"\\xueqiu\\cookie.txt"
        if os.path.exists(path):
            os.remove(path)

        with open(path, "w", encoding="utf-8") as f:
            f.write(json.dumps(cookie))
        browser.quit()
        main()


